/*
 * Created on May 12, 2005
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.placement.refresh.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.*;
import java.io.*;
import java.util.*;

/**
 * General Utility Base Object.
 * Creation date: (6/27/01 1:42:48 PM)
 * @author: fund/Eugene Backlin
 */
public class StringUtilities implements Serializable {
	private static final Log logger = LogFactory.getLog(StringUtilities.class);
	//
	//
	////////////////////////////
	//
	// Global Variables
	//
	////////////////////////////
	//
	//
	public boolean _debug = false;
	/**
	 * Utilities constructor comment.
	 */
	public StringUtilities() {
		super();
		setDebug(false);
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (2/22/02 4:06:58 PM)
	 * @return java.lang.Double
	 * @param d java.lang.Double
	 */
	public Double ceiling(Double d) {
		Double result = null;
		int id = d.intValue();
		Double nd = new Double(id);
		if (nd.doubleValue() < d.doubleValue()) {
			result = new Double(id + 1);
		} else {
			result = d;
		}
		return result;
	}

	/**
	 * Method isNumber.
	 * @param value
	 * @return boolean
	 */
	public boolean isNumber(String value) {
		/*
		final char[] numbers = value.toCharArray();
		  for (int ii = 0; ii < numbers.length; ii++) {      
			final char c = numbers[ii];
			if ((c >= '0') && (c <= '9')) continue;
			return false; // invalid
		  }
		  return true; // valid
		*/

		/*
		boolean isNumber = false;
		if (value != null)
		{
			String validChars = "0123456789";
		
			for (int ii = 0; ii < value.length() && isNumber; ii++) 
			{
				char c = value.charAt(ii); 
				if (validChars.indexOf(c) == -1) 
				{
					isNumber = false;
			  	}
			  	else
			  	{
					isNumber = true;
			  	}
			}
		}
		return isNumber;
		*/
		boolean iostat = true;
		int i = 0;
		if (value != null) {
			while ((iostat == true) && (i < value.length())) {
				if ((value.charAt(i) >= '0') && (value.charAt(i) <= '9')) {
					iostat = true;
				} else {
					iostat = false;
				}
				i++;
			}
		}
		return iostat;
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (10/21/02 9:25:31 AM)
	 * @return java.lang.String
	 */
	public String getDefaultPropertiesName(Object obj) {
		String name = null;
		Class objectClass = obj.getClass();
		String fullName = objectClass.getName();
		String className = fullName.substring(fullName.lastIndexOf('.') + 1);
		java.lang.reflect.Field[] fields = objectClass.getDeclaredFields();
		if (className != null) {
			name = className + ".properties";
		}
		return name;
	}

	/**
	 * Sets the Debug feature to (true) display debug statements
	 * or (flase) to hide the debug statements for each method.
	 * Creation date: (6/27/01 12:08:03 PM)
	 * @param iostat boolean
	 */
	public boolean getDebug() {
		return _debug;
	}

	/**
	 * Sets the Debug feature to (true) display debug statements
	 * or (flase) to hide the debug statements for each method.
	 * Creation date: (6/27/01 12:08:03 PM)
	 * @param iostat boolean
	 */
	public void setDebug(boolean iostat) {
		_debug = iostat;
	}

	/**
	 * Parse a delimeted string retunrnig a ArrayList.
	 * Creation date: (7/6/01 3:44:08 PM)
	 * @param str String
	 * @param delim String
	 * @return ArrayList 
	 */
	public ArrayList parseIt(String str, String delim) {
		ArrayList a = new ArrayList();
		if (str != null && str.length() > 0) {
			StringTokenizer parser = new StringTokenizer(str, delim);
			while (parser.hasMoreTokens()) {
				a.add(parser.nextToken());
			}
		}
		return a;
	}

	/**
	 * Method replace.
	 * @param buf
	 * @param match
	 * @param replaceWith
	 * @return String
	 */
	public String replace(String buf, String match, String replaceWith) {
		StringBuffer sb = new StringBuffer("");
		int p = 0;

		while (p < buf.length()) {
			int oldp = p;
			p = buf.indexOf(match, p);
			if (p == -1) {
				sb.append(buf.substring(oldp));
				break;
			}
			sb.append(buf.substring(oldp, p));
			sb.append(replaceWith);
			p += match.length();
		}
		return sb.toString();
	}

	/**
	 * Convert a delimeted string returning a String[] Array.
	 * using "|" as the delimeter
	 * Creation date: (7/6/01 3:44:08 PM)
	 * @param str   delimited String 
	 * @return String[] 
	 */
	public String[] stringToArray(String str) {
		return stringToArray(str, "|");
	}

	/**
	 * Convert a delimeted string returning a String[] Array.
	 * using "|" as the delimeter
	 * Creation date: (7/6/01 3:44:08 PM)
	 * @param str   delimited String 
	 * @param delim delimiter 
	 * @return String[] 
	 */
	public String[] stringToArray(String str, String delim) {
		ArrayList a = parseIt(str, delim);
		String[] s = arrayListToStringArray(a);

		return s;
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (5/31/02 9:54:30 AM)
	 * @return java.lang.Double
	 * @param amount java.lang.String
	 */
	public Double amtStringToDouble(String newDouble) {
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < newDouble.length(); i++) {
			String s = newDouble.substring(i, i + 1);
			if (!s.equals(",") && !s.equals("$")) {
				sb.append(s);
			}
		}
		newDouble = sb.toString();

		return (new Double(newDouble));
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (2/11/02 10:18:33 AM)
	 * @return java.lang.Double
	 * @param newDouble java.lang.String
	 */
	public Double stringToDouble(String newDouble) {
		return new Double(newDouble);
	}

	/**
	 * Convert a String Array returning a Double[] Array.
	 * Creation date: (7/6/01 3:44:08 PM)
	 * @param strArray String[]
	 * @return Double[] 
	 */
	public Double[] stringToDoubleArray(String[] strArray) {
		ArrayList a = new ArrayList();

		for (int i = 0; i < strArray.length; i++) {
			a.add(new Double(strArray[i]));
			if (getDebug()) {
				logger.debug("strArray[i] = [" + strArray[i] + "]");
			}
		}

		Double[] d = (Double[]) a.toArray(new Double[0]);

		return d;
	}

	/**
	 * Convert a delimited '|' String returning a Double[] Array.
	 * Creation date: (7/6/01 3:44:08 PM)
	 * @param strValue String
	 * @return Double[]
	 */
	public Double[] stringToDoubleArray(String strValue) {
		String delim = new String("|");

		return stringToDoubleArray(strValue, delim);
	}

	/**
	 * Convert a delimited String returning a Double[] Array.
	 * Creation date: (7/6/01 3:44:08 PM)
	 * @param strValue String
	 * @param delim String
	 * @return Double[]
	 */
	public Double[] stringToDoubleArray(String strValue, String delim) {
		String[] strArray = stringToArray(strValue, delim);
		ArrayList a = new ArrayList();

		for (int i = 0; i < strArray.length; i++) {
			a.add(new Double(strArray[i]));
			if (getDebug()) {
				logger.debug("strArray[i] = [" + strArray[i] + "]");
			}
		}

		Double[] d = (Double[]) a.toArray(new Double[0]);

		return d;
	}

	/**
	 * Convert a Double Array returning a String[] Array.
	 * Creation date: (7/6/01 3:44:08 PM)
	 * @param dbl Double[]
	 * @return String[]
	 */
	public String[] doubleToStringArray(Double[] dbl) {
		ArrayList a = new ArrayList();
		for (int i = 0; i < dbl.length; i++) {
			a.add(dbl[i].toString());
			if (getDebug()) {
				logger.debug("dbl[i].toString() = [" + dbl[i].toString() + "]");
			}
		}

		String[] s = arrayListToStringArray(a);

		return s;
	}

	/**
	 * Convert a Double Array returning a String[] Array.
	 * Creation date: (7/6/01 3:44:08 PM)
	 * @param dbl Double[]
	 * @param pefix int
	 * @param suffix int
	 * @return String[]
	 */
	public String[] doubleToStringArray(Double[] dbl, int prefix, int suffix) {
		NumberFormat numberFormatter = NumberFormat.getInstance();

		if (prefix > 0) {
			numberFormatter.setMinimumIntegerDigits(prefix);
		}

		if (suffix > 0) {
			numberFormatter.setMinimumFractionDigits(suffix);
		}

		ArrayList a = new ArrayList();
		for (int i = 0; i < dbl.length; i++) {
			a.add(numberFormatter.format(dbl[i]).toString());
			if (getDebug()) {
				logger.debug(
					"numberFormatter.format(dbl[i]).toString() = ["
						+ numberFormatter.format(dbl[i]).toString()
						+ "]");
			}
		}

		String[] s = arrayListToStringArray(a);

		return s;
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (2/11/02 10:20:14 AM)
	 * @return java.lang.Integer
	 * @param newInteger java.lang.String
	 */
	public Integer stringToInteger(String newInteger) {
		return new Integer(newInteger);
	}

	/**
	 * Convert a delimited '|' String returning a Integer[] Array.
	 * Creation date: (7/6/01 3:44:08 PM)
	 * @param strValue String
	 * @return Integer[]
	 */
	public Integer[] stringToIntegerArray(String strValue) {
		String delim = new String("|");

		return stringToIntegerArray(strValue, delim);
	}

	/**
	 * Convert a delimited String returning a Integer[] Array.
	 * Creation date: (7/6/01 3:44:08 PM)
	 * @param strValue String
	 * @param delim String
	 * @return Integer[]
	 */
	public Integer[] stringToIntegerArray(String strValue, String delim) {
		String[] strArray = stringToArray(strValue, delim);
		ArrayList a = new ArrayList();

		for (int i = 0; i < strArray.length; i++) {
			a.add(new Integer(strArray[i]));
			if (getDebug()) {
				logger.debug("strArray[i] = [" + strArray[i] + "]");
			}
		}

		Integer[] d = (Integer[]) a.toArray(new Integer[0]);

		return d;
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (5/20/02 3:32:27 PM)
	 * @param a java.lang.String[]
	 */
	public String buildStringFromArray(String[] a) {
		StringBuffer b = new StringBuffer();
		for (int i = 0; i < a.length; i++) {
			b.append(a[i]);
		}
		return b.toString();
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (5/20/02 1:55:55 PM)
	 * @return java.lang.String
	 * @param a java.lang.String[]
	 */
	public String buildDelimitedStringFromArray(String[] a) {
		return buildDelimitedStringFromArray(a, "|");
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (5/20/02 3:23:25 PM)
	 * @return java.lang.String
	 * @param a java.lang.String[]
	 * @param delim java.lang.String
	 */
	public String buildDelimitedStringFromArray(String[] a, String delim) {
		StringBuffer b = new StringBuffer();
		for (int i = 0; i < a.length; i++) {
			String str = a[i] + delim;
			b.append(str);
		}
		return b.toString();
	}

	/**
	 * Method arrayListToStringArray.
	 * @param list
	 * @return String[]
	 */
	public String[] arrayListToStringArray(ArrayList list) {
		String[] szArray = (String[]) (list.toArray(new String[0]));
		return szArray;
	}

	/**
	 * Method convertToBufferedReader.
	 * @param input
	 * @return BufferedReader
	 */
	public BufferedReader convertToBufferedReader(InputStream input) {
		InputStreamReader isr = new InputStreamReader(input);
		return new BufferedReader(isr);
	}

	/**
	 * Method convertToStringArray.
	 * @param input
	 * @return String[]
	 */
	public String[] convertToStringArray(InputStream input)
		throws IOException {
		InputStreamReader isr = new InputStreamReader(input);
		BufferedReader br = new BufferedReader(isr);
		String line = null;
		ArrayList list = new ArrayList();
		while ((line = br.readLine()) != null) {
			list.add(line);
		}
		br.close();
		return arrayListToStringArray(list);
	}

	/**
	 * Method getFileToArray.
	 * @param reportPath
	 * @param fileBeingRead
	 * @return String[]
	 */
	public String[] getFileToArray(String reportPath, String fileBeingRead) {
		return getFileToArray(fileBeingRead);
	}

	/**
	 * Get File and return a String[] with its contents.
	 * Creation date: (7/31/01 2:04:46 PM)
	 * @return java.lang.String[]
	 */
	public String[] getFileToArray(String fileBeingRead) {
		ArrayList a = new ArrayList();
		String[] x = null;
		//
		////////
		// Get File Information
		////////
		//
		try {
			BufferedReader inRateInterpolate =
				new BufferedReader(new FileReader(new File(fileBeingRead)));
			String fileText = inRateInterpolate.readLine();
			a.add(fileText);

			while (fileText != null) {
				if (getDebug()) {
					logger.debug("fileText = [" + fileText + "]");
				}
				fileText = inRateInterpolate.readLine();
				if (fileText != null) {
					a.add(fileText);
				}
			}

			inRateInterpolate.close();
		} catch (FileNotFoundException fnfe) {
			logger.debug(fnfe);
			return x;
		} catch (IOException ioe) {
			logger.debug(ioe);
		}
		x = arrayListToStringArray(a);
		return x;
	}

	/**
	 * Method writeArrayToFile.
	 * @param list
	 * @param fileBeingSaved
	 * @throws IOException
	 */
	public void writeArrayToFile(String[] list, String fileBeingSaved)
		throws IOException {
		PrintWriter out =
			new PrintWriter(new BufferedWriter(new FileWriter(fileBeingSaved)));
		for (int i = 0; i < list.length; i++) {
			out.println(list[i]);
		}
		out.close();
	}

	/**
	 * Method appendArrayToFile.
	 * @param list
	 * @param fileBeingSaved
	 * @throws IOException
	 */
	public void appendArrayToFile(String[] list, String fileBeingSaved)
		throws IOException {
		PrintWriter out =
			new PrintWriter(
				new BufferedWriter(new FileWriter(fileBeingSaved, true)));
		for (int i = 0; i < list.length; i++) {
			out.println(list[i]);
		}
		out.close();
	}
	public String strippedComma(String str) {
		if (str == null)
			return "";
		StringTokenizer tokens = new StringTokenizer(str, ",");
		if (tokens.countTokens() == 0)
			return str;
		String tmp = "";
		while (tokens.hasMoreTokens()) {
			tmp += tokens.nextToken();
		}
		return tmp;
	}

	public String removeTrailingZeros(String str) {
		if (str.indexOf(".") > -1) {
			while (str.charAt(str.length() - 1) == '0') {
				str = str.substring(0, str.length() - 1);
			}

			if (str.charAt(str.length() - 1) == '.') {
				str = str.substring(0, str.length() - 1);
			}
		}

		return str;
	}
}